package com.sos.core.game
{
	import com.sos.core.DataObject;
	import com.sos.logging.Logger;

	public class GameProc extends DataObject
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// Property names
		protected static const		JSON_PROPERTY_LABEL:String				= "label";
		protected static const		JSON_PROPERTY_SOURCE:String				= "source";
		protected static const		JSON_PROPERTY_SOURCEPLAYER:String		= "sourcePlayer";
		protected static const		JSON_PROPERTY_SOURCECARD:String			= "sourceCard";
		protected static const		JSON_PROPERTY_EFFECTS:String			= "effects";		
		
		// Default values
		protected static const      DEFAULT_LABEL:String					= "";
		protected static const      DEFAULT_SOURCE:uint						= DataObject.INVALID_ID;
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var	_label:String;
		private var	_sourceId:uint; 				// = 0 if basic player ability, instanceId of buff/gear/debuff otherwise
		private var	_sourcePlayer:uint;
		private var	_sourceCard:uint;
		private var _effects:Vector.<GameCastEffect>;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameProc(data:Object = null)
		{
			super();
			// Init objects
			_effects = new Vector.<GameCastEffect>();
			// clears data
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------	
		
		public function get label():String 
		{
			return _label;
		}
		
		public function set label(value:String):void 
		{
			_label = value;
		}
		
		public function get sourceId():uint 
		{
			return _sourceId;
		}
		
		public function set sourceId(value:uint):void 
		{
			_sourceId = value;
		}
		
		public function get effects():Vector.<GameCastEffect> 
		{
			return _effects;
		}
		
		public function set effects(value:Vector.<GameCastEffect>):void 
		{
			_effects = value;
		}
		
		public function get sourcePlayer():uint 
		{
			return _sourcePlayer;
		}
		
		public function set sourcePlayer(value:uint):void 
		{
			_sourcePlayer = value;
		}
		
		public function get sourceCard():uint 
		{
			return _sourceCard;
		}
		
		public function set sourceCard(value:uint):void 
		{
			_sourceCard = value;
		}
		
		
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		override protected function validate(data:Object):void
		{
			// Base properties
			if (!data.hasOwnProperty(JSON_PROPERTY_LABEL)) 			throwLoadMissingPropError(JSON_PROPERTY_LABEL);
			
		}
		
		override protected function fill(data:Object):void
		{			
			
			_label 			= data[JSON_PROPERTY_LABEL];
			_sourceId 		= int(data[JSON_PROPERTY_SOURCE]);
			_sourcePlayer 	= int(data[JSON_PROPERTY_SOURCEPLAYER]);
			_sourceCard 	= int(data[JSON_PROPERTY_SOURCECARD]);
			// Temporary effects list
			var effects:Vector.<GameCastEffect> = new Vector.<GameCastEffect>;
			// Check if JSON_PROPERTY_EFFECTS exists 
			if (data.hasOwnProperty(JSON_PROPERTY_EFFECTS))
			{
				// Loop over raw effects
				for (var effect:String in data[JSON_PROPERTY_EFFECTS])
				{
					// Loop over effects targets array
					var raw_effect_targets:Array = data[JSON_PROPERTY_EFFECTS][effect];
					for (var j:int = 0; j<raw_effect_targets.length; j++)
					{
						effects.push(new GameCastEffect(effect, raw_effect_targets[j]));
					}
				}
			}
			// Clear and popolate effects list
			_effects.splice(0, _effects.length);
			for (var i:int = 0; i < effects.length; i++)
			{
				_effects.push(effects[i]);
			}
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		override public function clear():void
		{
			// Clear data
			_label			= DEFAULT_LABEL;
			_sourceId		= DEFAULT_SOURCE;
			_sourcePlayer	= DEFAULT_SOURCE;
			_sourceCard		= DEFAULT_SOURCE;
			
			// Clear objects
			_effects.splice(0, _effects.length);
		}


	}
}